package com.logos.reflect;

import com.logos.entity.Student;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Parameter;

public class FieldTest {

    public static void main(String[] args) throws Exception{
        // 获取Class对象
        Class<?> clazz = Class.forName("com.logos.entity.Student");
        // 获取所有成员变量
        Field[] declaredFields = clazz.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println(declaredField);
        }
        // 获取单个成员变量
        Field name = clazz.getDeclaredField("name");
        System.out.println(name);
        // 获取权限修饰符
        int modifiers = name.getModifiers();
        System.out.println("modifiers = " + modifiers);
        // 获取成员变量名
        String FieldName = name.getName();
        System.out.println("FieldName = " + FieldName);
        // 获取成员变量类型
        Class<?> type = name.getType();
        System.out.println("type = " + type);
        // 获取成员变量记录的值
        Student stu = new Student("logos",19);
        // 暴力反射，取消访问检查
        name.setAccessible(true);
        Object value = name.get(stu);
        System.out.println("value = " + value);
        // 修改成员变量记录的值
        name.set(stu,"bsJTB");
        System.out.println(stu);
    }
}
